import { SearchLDCComponent } from './../searchLDC/searchLDC.component';
import { LoginComponent } from './../storeLogin/login.component';
import { NavController } from 'ionic-angular';
import { MdIconRegistry } from '@angular/material';
import { FNCManager } from './../../common/fncManager';
import { LDCServiceNode } from './../../service/system.service';
import { NavigateService, SystemService } from './../../service/index';
import { Component, OnInit } from '@angular/core';
import { MessageBoxService, MessageResult, GrowlMessageType } from '../../component/messageBoxService';

@Component({
    //moduleId: module.id,
    selector: 'check-env',
    templateUrl: './checkEnv.component.html',
    styleUrls: ['./checkEnv.component.scss']
})

export class CheckEnvComponent implements OnInit {
    constructor(
        private router: NavigateService,
        private sysSvr: SystemService,
        private msgSvr: MessageBoxService,
        public navCtrl: NavController
    ) { }
    title: string = "检测门店业务服务";
    isChecking: boolean = true;
    checkArrayList: Array<LDCServiceNode> = [];
    isPass: boolean = true;
    IPaddress = this.sysSvr.LDCAddress;
    store;
    ngOnInit() {
        this.checkFBS();
        this.checkFNS();
    }

    /**
     * 重新检测
     */
    reCheck() {
        if (this.isChecking) {
            return;
        }
        // this.isChecking = true;
        // this.isPass = true;
        // this.sysSvr.CheckLDCService();
        this.checkFBS();
    }

    /**
     * 判断环境检测是否通过
     */
    checkIsPass() {
        this.isPass = true;
        for (let item of this.checkArrayList) {
            if (item.status == "failed") {
                this.isPass = false;
            }
        }
        if (this.isPass) {
            let terminal = localStorage.getItem("terminal");
            if (terminal) {
                this.router.navigate(["employeeLogin"]);
            } else {
                this.router.navigate(["bindDevice"]);
            }

        }
        this.isChecking = false;
    }

    /**
     * 检测是否开启FBS
     */
    async checkFBS() {
        this.isChecking = true;
        try {
            let result = await this.sysSvr.checkIsFBS();
            if (result) {
                this.store = result;
                this.isPass = true;
                this.isChecking = false;
                // this.router.navigate(["storeLogin"]);
                this.navCtrl.setRoot(LoginComponent);
            } else {
                this.isPass = false;
                this.isChecking = false;
            }
        } catch (error) {
            this.isPass = false;
            this.isChecking = false;
        }

    }

    private checkFNS() {
        EventRegistHandle.Call("FNSConnect");
    }

    goBack() {
        this.router.navigate(["selectStore"]);
    }

    reSetLDC() {
        // this.router.navigate(["searchLDC", "manual"]);
        this.navCtrl.setRoot(SearchLDCComponent, {
            searchMode: "manual"
        })
    }
}

declare let EventRegistHandle: any;